package com.qfedu.spring.mvc.controller;

import com.qfedu.spring.mvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @auther asus
 * @date 2022/4/29 17:45
 */
@Controller
@RequestMapping("data")
//model中的name和age会存到session中
@SessionAttributes({"name","age"})
public class DataController {
    //原始传值方式
    @RequestMapping("test1")
    public String test1(HttpServletRequest request, HttpSession session){
        //到request域中存数据
        request.setAttribute("name","胡长攀");
        request.setAttribute("age","13");
        //session中存数据
        session.setAttribute("date","2022");
        return "welcome";
    }

    //spring推荐传值方式
    @RequestMapping("test2")
    public String test2(Model model){
        //model中的数据是放到request中的
        model.addAttribute("name","胡长攀");
        model.addAttribute("age",18);
        //如果没有指定key的话，key默认就是value值的类型
        model.addAttribute(1234511);
        User user=new User();
        user.setName("憨憨");
        user.setAge(18);
        model.addAttribute(user);
        return "welcome";
    }

    @RequestMapping("test3")
    public ModelAndView  test3(){
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("name","胡");
        return modelAndView ;
    }
}
